/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boom;

/**
 *
 * @author kaaz
 */
public class Tree {
   
    public Node root = null;
    public int size = 0;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Tree tree = new Tree();
        String[] names = {
            "h","i","j","k","l","m","n","o","p","q","r","s","a","b","c","d","e","f","g"
        };
        for(String name : names){
            tree.add(name);
        }
        tree.printTree();
    }
    
    public void add(String text){
        this.add(new Node(text));
        if(this.root.parent != null){
            this.root = this.root.parent;
        }
	printTree();
    }
    
    public void add(Node n){
        if(root == null){
            root = n;
            n.height = 0;
            this.size++;
	    System.out.println("Adding '"+n.value+"'.");
        }
        else{
            if(root.add(n)){
                this.size++;
            }
	}
    }
    
    private void printTree(){
        System.out.println("Printing tree!");
        if(this.root != null){
            this._printChilds(root, "");
        }
        System.out.println("");
    }
    
    private void _printChilds(Node p, String prefix){
        System.out.println(prefix + '-' + p.value + " ("+p.getBalanceValue()+
            " (L" + p.countLeftHeight() + ")(R" + p.countRightHeight() + "))");
        if(p.left != null){
            this._printChilds(p.left, prefix + "L");
        }
        if(p.right != null){
            this._printChilds(p.right, prefix + "R");
        }
    }
}
